<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php');
require_once('../../../include/inc_config.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if(isset($_POST['MM_update']))
	{
		if((isset($_POST['Code_garantie']) && $_POST['Code_garantie'])!=NULL)
		{
			$verif = veriflibelle1("Code_garantie");
			if($verif)
			{
                 $iValidUpdate = 0 ;
                 $oGarantie = new dataObject_garantie($_POST['Identifiant_garantie']);
                 
                 // --- la modification ne concerne ni le code garantie ni le produit
                 if ($oGarantie->_CODE_GARANTIE == $_POST['Code_garantie'] && $oGarantie->_IDENTIFIANT_PRODUIT == $_POST['Identifiant_produit'])
                 {
                    $iValidUpdate = 1 ;
                 }
                 
                 // --- la modification ne concerne le code garantie et ou le produit
                 else
                 {
                    $toCriterias['Code_garantie'] = $_POST['Code_garantie'] ;
                    $toCriterias['Identifiant_produit'] = $_POST['Identifiant_produit'] ;
                    $nums = existeEnregistrement($database_microassur, $microassur, "garantie", $toCriterias) ;
                    if ($nums>=intval(1))
                    {
                        $message = colorer("Cette garantie existe déjà pour ce produit", "false") ;
                    }
                    else
                    {
                        $iValidUpdate = 1 ;
                    }
                 }
                 
					if($iValidUpdate)
						{
							$veriflibelle=verifLibelle("Libelle_garantie") && verifLibelle1("Code_garantie");
								if($veriflibelle==TRUE)
								{
									if($_POST['Type_garantie']!=NULL && $_POST['Identifiant_produit']!=NULL )
									{
										if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) 
										{
										 $updateSQL = sprintf("UPDATE garantie SET Code_garantie=%s, Libelle_garantie=%s, Type_garantie=%s, Identifiant_produit=%s WHERE Identifiant_garantie=%s",
															   GetSQLValueString($_POST['Code_garantie'], "text"),
															   GetSQLValueString($_POST['Libelle_garantie'], "text"),
															   GetSQLValueString($_POST['Type_garantie'], "text"),
															   GetSQLValueString($_POST['Identifiant_produit'], "int"),
															   GetSQLValueString($_POST['Identifiant_garantie'], "int"));
										 mysql_select_db($database_microassur, $microassur);
										   mysql_query("START TRANSACTION");
										 $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
											if($Result1==intval(1)){mysql_query("commit");
												$message = colorer("La modification de la garantie est prise en compte","true");}
											else
												{ mysql_query("rollback");$message = colorer("Erreur: Contacter la DSI ","false");}
										}
									}
									else
										$message="";
																		}
								else
									$message = colorer("Vous devez saisir le libellé","false");
						}
			}
			else
				$message = colorer("Le code saisi comporte des caractères spéciaux interdits","false");
		}
		else
			$message = colorer("Vous devez saisir le code","false");
}
$colname_rs = "-1";
if (isset($_GET['id'])) {
  $colname_rs = $_GET['id'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs = sprintf("SELECT * FROM garantie WHERE Identifiant_garantie = %s", GetSQLValueString($colname_rs, "int"));
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);

$totalRows_rs = mysql_num_rows($rs);

mysql_select_db($database_microassur, $microassur);
$query_rs2 = "SELECT Identifiant_produit, Libelle_produit FROM produit";
$rs2 = mysql_query($query_rs2, $microassur) or die(mysql_error());
$row_rs2 = mysql_fetch_assoc($rs2);
$totalRows_rs2 = mysql_num_rows($rs2);

/**************************************************************************************/
$query_rs_dico1 = 'SELECT Identifiant_table FROM dictionnaire where Code_table="T0001"';
$rs_dico1 = mysql_query($query_rs_dico1, $microassur) or die(mysql_error());
$row_rs_dico1 = mysql_fetch_assoc($rs_dico1);
$num_row = $row_rs_dico1['Identifiant_table'];
/**************************************************************************************/
$query_rs_dico2 = "SELECT Code_element, Nom_element FROM elementdico where Identifiant_table = $num_row";
$rs_dico2 = mysql_query($query_rs_dico2, $microassur) or die(mysql_error());
$row_rs_dico2 = mysql_fetch_assoc($rs_dico2);


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
			<link rel="stylesheet" type="text/css" href="../offre.css" />
			<!--[if IE 7]><link rel="stylesheet" type="text/css" href="../../../css/ie7.css" /><![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Garantie";
					$nommodule2="Détail garantie";
					?>
			</div>
			<div class="cleared"></div>
			<!-- /*******************************/ -->
			<div class="corps1">

			<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
				<div class="imgleft"></div>
				<div class="imgmiddle">
					<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
					<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
						<div class="gar">
						
							  <table>
									<tr>
								  <td>CODE</td>
									  <td><input class="resize_1" type="text" name="Code_garantie" value="<?php if(isset($_POST['Code_garantie'])) echo remind_text('Code_garantie'); else echo htmlentities($row_rs['Code_garantie'], ENT_COMPAT, 'utf-8'); ?>" size="15" MAXLENGTH="5" /></td>
									</tr>
									<tr>
									  <td>LIBELLE</td>
									  <td><input type="text" name="Libelle_garantie"  maxlength="30" value="<?php if(isset($_POST['Libelle_garantie'])) echo remind_text('Libelle_garantie'); else echo htmlentities($row_rs['Libelle_garantie'], ENT_COMPAT, 'utf-8'); ?>" size="50" /></td>
									</tr>
									<tr>
									  <td>TYPE</td>
									  <td><select name="Type_garantie">
										 <?php do { ?>
												<option value="<?php echo $row_rs_dico2['Code_element']; ?>" <?php if (!(strcmp($row_rs_dico2['Code_element'], htmlentities($row_rs['Type_garantie'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?> ><?php echo $row_rs_dico2['Nom_element']; ?></option>
										<?php } while($row_rs_dico2 = mysql_fetch_assoc($rs_dico2)); ?>
									  </select>
									  </td>
									</tr>
									<tr>
									  <td>PRODUIT</td>
									  <td><select name="Identifiant_produit">
										<?php 
								do {  
								?>
										<option value="<?php echo $row_rs2['Identifiant_produit']?>" <?php if (!(strcmp($row_rs2['Identifiant_produit'], htmlentities($row_rs['Identifiant_produit'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>><?php echo $row_rs2['Libelle_produit']?></option>
										<?php
								} while ($row_rs2 = mysql_fetch_assoc($rs2));
								?>
									  </select>
									  </td>
									</tr>
								  </table>
								</div>
						</div>
						<div class="imgright"></div>				
							<div class="cleared"></div>
							
								<div class="bt1"><a href="./index.php"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
								<div class="bt"><span class="btleft"></span><input type="submit" name="Modifier" value="Modifier" class="resizeall btmiddle"/><span class="btright"></span></div>
								
							<div class="cleared"></div>
				<input type="hidden" name="Identifiant_garantie" value="<?php echo $row_rs['Identifiant_garantie']; ?>" />
				<input type="hidden" name="MM_update" value="form1" />
			</form>
		</div>
</body>
</html><?php ob_end_flush();?>
<?php
mysql_free_result($rs);

mysql_free_result($rs2);
?>
